﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Helpers
{
    #region Using
    using System.Web.Mvc;
    using System.Collections;
    using System.Net;
    using System.IO;
    #endregion

    public static class HtmlHelpers
    {
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (input.Length > length)
            {
                input = input.Substring(0, length);
            }

            int lastPos = input.LastIndexOf(" ");
            if (lastPos == -1)
                return "...";
            return input.Substring(0, lastPos) + "...";
        }

        public static ArrayList RetrieveWhether()
        {
            int i = 0;
            string strUrl = null;

            ArrayList strURLArray = new ArrayList(8);
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/SonLa.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/HaiPhong.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/Hanoi.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/Vinh.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/DaNang.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/NhaTrang.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/Pleicu.xml");
            strURLArray.Add("http://www.vnexpress.net/ListFile/Weather/HCM.xml");

            ArrayList strHTMLResutl = new ArrayList();
            for (i = 0; i <= 7; i++)
            {
                try
                {
                    strUrl = (String)strURLArray[i];
                    WebRequest requestHTml = WebRequest.Create(strUrl);
                    WebResponse responseHTml = requestHTml.GetResponse();
                    StreamReader r = new StreamReader(responseHTml.GetResponseStream(), System.Text.UTF8Encoding.UTF8);
                    string htmlContent = r.ReadToEnd();
                    r.Close();
                    htmlContent = htmlContent.Replace("<AdImg>", "<Img src=http://www.vnexpress.net/Images/Weather/").Replace("</AdImg>", ">").Replace("<![CDATA[", "").Replace("]]>", "");
                    htmlContent = htmlContent.Replace("<AdImg1>", "<Img src=http://www.vnexpress.net/Images/Weather/").Replace("</AdImg1>", ">");
                    htmlContent = htmlContent.Replace("<AdImg2>", "<Img src=http://www.vnexpress.net/Images/Weather/").Replace("</AdImg2>", "><Img src=http://www.vnexpress.net/Images/Weather/c.gif><br>");
                    htmlContent = htmlContent.Replace("<AdImg3></AdImg3>", "");
                    htmlContent = htmlContent.Replace("<AdImg4></AdImg4>", "");
                    htmlContent = htmlContent.Replace("<AdImg5></AdImg5>", "");

                    strHTMLResutl.Add(htmlContent);
                }
                catch
                {

                }
            }

            return strHTMLResutl;
        }
    }
}